import streamlit as st
import glob, os, re
from caption import ImgCaption
from tts import text_to_speech
import time
from extract import extract_key_frames

def main():
    
    PATH_VIDEO = './video/'
    PATH_CLIPS = './image/'
    
    cap = ImgCaption()

    
    st.title('云信AI：视觉障碍者的心目电影院')
    st.sidebar.title("请选择或者上传视频，以便自动生成描述")
    option = st.sidebar.selectbox('请选择一个内置的视频',('','体育短片', '综艺节目', '电影片段'))
    uploaded_file = st.sidebar.file_uploader("或者可以上传一个视频", type='mp4')
    st.subheader("当前正在播放的视频...")
    
    if option == '体育短片':
        mp4_path = os.path.join(PATH_VIDEO,'test002.mp4')
        img_paths = os.path.join(PATH_CLIPS,'test002*.jpeg')
    elif option == '综艺节目':
        mp4_path = os.path.join(PATH_VIDEO,'test001.mp4')
        img_paths = os.path.join(PATH_CLIPS,'test001*.jpeg')
    elif option == '电影片段':
        mp4_path = os.path.join(PATH_VIDEO,'test003.mp4')
        img_paths = os.path.join(PATH_CLIPS,'test003*.jpeg')
    
    
    if uploaded_file is not None:
        with open(os.path.join("./video",uploaded_file.name),"wb") as f: 
            f.write(uploaded_file.getbuffer())     
        # st.video(uploaded_file)
        mp4_path = os.path.join("./video",uploaded_file.name)
        img_paths = os.path.join(PATH_CLIPS,'%s*.jpeg' % uploaded_file.name.split('.')[0])
        print(uploaded_file.name)
        
    if (option == '') and (uploaded_file is None):
        st.stop()
    
    # print(img_paths)
    st.video(mp4_path)
    st.subheader("Step1：分析视频得到关键帧（共12帧）")
    with st.spinner('正在对视频分析，抽取关键帧'):
        # time.sleep(2)
        extract_key_frames(mp4_path)
    img_paths = glob.glob(img_paths)
    img_paths = sorted(img_paths, key=lambda x: int(re.match('\d+',x.split('_')[1])[0]))
    col1, col2, col3 = st.columns(3)
    # col1.subheader("当前正在播放的视频")
    # col2.caption("截图分析的结果")
    # col2.text('一个正在打电话的女人')
    # col1.text("正在播放的视频...")
    for i in range(0,12,3):
        col1.image(img_paths[i])
        col2.image(img_paths[i+1])
        col3.image(img_paths[i+2])
    
    # col2.text_area(label='截图分析的结果',value='一个正在打电话的女人',height=155)
    st.subheader("Step2：生成关键帧的描述")
    col4, col5, col6 = st.columns(3)
    desc = []
    with st.spinner('正在对关键帧生成文字描述'):
        for i in range(0,12,3):
            text1 = cap.img2txt(img_paths[i]).replace('。','')
            text2 = cap.img2txt(img_paths[i+1]).replace('。','')
            text3 = cap.img2txt(img_paths[i+2]).replace('。','')
            # text1 = text2 = text3 = '一个人拿着剪刀的特写'
            
            col4.text_area('第%d帧' %(i+1), text1)
            col5.text_area('第%d帧' %(i+2), text2)
            col6.text_area('第%d帧' %(i+3), text3)
            
            desc.append('，'.join([text1, text2, text3]))
        
    st.subheader("Step3：合并描述并转成音频")
    st.text_area('以下是整段视频的描述', '本段视频包含以下内容，'+'，'.join(desc), height=120)
    text_to_speech('，'.join(desc))
    st.subheader("Step4：将音频通过网易云信传到云端点播")
    st.code('''
from vcloud import Client

AppKey = '****'
AppSecret = '****'
client = Client(AppKey, AppSecret)

#上传初始化的请求包体参数
body = {"originFileName":"result.wav"}

#要上传文件的本地路径
localfile = './audio/result.wav'
res = client.upload_file(body, localfile)

#查询视频的请求包体参数
body = {"objectNames":["result.wav"]}

res = client.query_id(body)
if res != None:
    print("client.query_id res : {0}".format(vars(res)))
else:
    print("query video error!")
    ''')
    st.subheader("成功！使用网易云信点播音频")
    st.audio('./audio/result.wav')
    # st.audio('http://jdvodc7dmuqnp.vod.126.net/jdvodc7dmuqnp/c33bdc31dae04ce9b60882dbfbd93eb9.wav')
    # st.button('提交弹幕')
    

if __name__ == "__main__":
    main()